package com.xjtu.duansheli.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.duansheli.entity.IdentifyImagesInfo;
import com.xjtu.duansheli.entity.ProductImagesInfo;
import com.xjtu.duansheli.exception.MyException;
import com.xjtu.duansheli.mapper.ProductImagesInfoMapper;
import com.xjtu.duansheli.service.ProductImagesInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjtu.duansheli.utils.DocumentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Service
public class ProductImagesInfoServiceImpl extends ServiceImpl<ProductImagesInfoMapper, ProductImagesInfo> implements ProductImagesInfoService {

    @Autowired
    private ProductImagesInfoMapper productImagesInfoMapper;

    @Override
    public List<ProductImagesInfo> getAllProductImages(int current, int size) {
        Page<ProductImagesInfo> page = new Page<>(current,size);
        productImagesInfoMapper.selectPage(page, null);
        System.out.println(page.getTotal());
        return page.getRecords();
    }

    @Override
    public ProductImagesInfo getProductImageByid(Long id) {
        ProductImagesInfo productImagesInfo = productImagesInfoMapper.selectById(id);
        if (productImagesInfo == null) {
            throw new MyException(500, "不存在该商品图片！");
        }
        return productImagesInfo;
    }

    @Override
    public List<ProductImagesInfo> getProductImageBypid(Long pid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("pid",pid);

        List<ProductImagesInfo> productImagesInfos = productImagesInfoMapper.selectByMap(map);
        return productImagesInfos;
    }

    @Override
    public int addProductImages(MultipartFile[] files, Long pid) {
        int result = 0;
        try{
            if (files != null && files.length > 0) {

                for (MultipartFile file : files) {
                    String fileName = file.getOriginalFilename();
                    assert fileName != null;
                    ProductImagesInfo productImagesInfo = new ProductImagesInfo();
                    if (fileName.endsWith("jpg") || fileName.endsWith("png") || fileName.endsWith("jpeg")) {
                        Map<String, String> temps = DocumentUtil.saveDocument(fileName);
                        String savePath = temps.get("filePath");
                        String saveUrl = temps.get("fileUrl");
                        productImagesInfo.setImageName(fileName)
                                .setImageUrl(saveUrl)
                                .setImagePath(savePath)
                                .setPid(pid);
                        file.transferTo(new File(savePath));
                        result += productImagesInfoMapper.insert(productImagesInfo);
                    } else {
                        throw new MyException(400, "文件"+fileName+"格式错误！请上传正确的图片文件！");
                    }

                }
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            throw new MyException(500, "添加认证图片失败！");
        }
        return result;
    }

    @Override
    public int deleteProductImages(Long id) {
        try {
            return productImagesInfoMapper.deleteById(id);
        } catch (Exception e) {
            throw new MyException(500, "删除商品图片失败！");
        }
    }

    @Override
    public int getTotal() {
        return productImagesInfoMapper.selectCount(null);
    }
}
